#!/usr/bin/env zsh

# Log functions [[[
function error(){ print -P "%F{red}ZUNIT[ERROR]: $1%f" && return 1; }
function info() { print -P "%F{blue}ZUNIT[INFO]%f: %F{cyan}$1%f"; }
function warn() { print -P "%F{yellow}ZUNIT[WARN]: $1%f"; }
# ]]]
# Create Zunit test environment [[[
GIT_REPO=$(git rev-parse --show-toplevel)
typeset -gAH  ZINIT;
ZINIT[HOME_DIR]="$GIT_REPO/tests/_support/zunit";    ZINIT[BIN_DIR]=$ZINIT[HOME_DIR]/zinit.git;
ZINIT[COMPLETIONS_DIR]=$ZINIT[HOME_DIR]/completions; ZINIT[SNIPPETS_DIR]=$ZINIT[HOME_DIR]/snippets;
ZINIT[ZCOMPDUMP_PATH]=$ZINIT[HOME_DIR]/zcompdump;    ZINIT[PLUGINS_DIR]=$ZINIT[HOME_DIR]/plugins;
ZPFX=$ZINIT[HOME_DIR]/polaris;

rm -rf $ZINIT[HOME_DIR]
mkdir -p $ZINIT[HOME_DIR]
git diff > $ZINIT[HOME_DIR]/unstaged.diff
info 'creating test env' \
  && git clone \
      --dissociate \
      --local \
      --no-hardlinks \
      --reference "$GIT_REPO" \
      "$GIT_REPO" \
    "$GIT_REPO"/tests/_support/zunit/zinit.git \
  || error 'failed to create zinit copy for test env' \
  &&  [[ -s $ZINIT[HOME_DIR]/unstaged.diff ]] \
  && git -C $ZINIT[BIN_DIR] apply $ZINIT[HOME_DIR]/unstaged.diff \
  && info 'installing zinit' \
  && command chmod g-rwX $ZINIT[HOME_DIR] \
  && zcompile $ZINIT[BIN_DIR]/zinit.zsh \
  && info 'successfully created zunit env' \
  || error 'failed to create zunit env'

source $ZINIT[BIN_DIR]/zinit.zsh \
  && autoload -Uz _zinit \
  && (( ${+_comps} )) \
  && _comps[zinit]=_zinit
# ]]]
# Install Annexes [[[
info 'installing test dependencies'
    # ver'fix/improve-lbin-logic' \
zinit depth'1' light-mode for \
  zdharma-continuum/zinit-annex-binary-symlink \
  zdharma-continuum/zinit-annex-{'bin-gem-node','default-ice'}
# ]]]

# vim:ft=zsh:sw=2:sts=2:et:foldmarker=[[[,]]]:foldmethod=marker
